//
//  MWMineHeaderView.m
//  XiRan-iOS
//
//  Created by Richfit on 2019/10/12.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "MWMineHeaderView.h"

@interface MWMineHeaderView ()

@property(nonatomic, strong) UIView *topBGView;
@property(nonatomic, strong) UIView *bottomBGView;

@property(nonatomic, strong) UIView *menuView;
@property(nonatomic, strong) UIButton *messageBtn;
@property(nonatomic, strong) UIButton *collectBtn;
@property(nonatomic, strong) UIButton *languageBtn;
@property(nonatomic, strong) UIButton *aboutBtn;

@end

@implementation MWMineHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    if (!(self = [super initWithFrame:frame])) {
        return nil;
    }
    
    [self setupPageSubviews];
    [self layoutPageSubviews];
    
    return self;
}

- (void)onClickAction:(UIButton *)sender {
    if (self.block) {
        self.block(sender.tag);
    }
}

- (void)setAvatarUrl:(NSString *)avatarUrl {
    if (avatarUrl && avatarUrl.length) {
        [_avatarBtn sd_setImageWithURL:[NSURL URLWithString:[avatarUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] forState:UIControlStateNormal];
    }
}

- (void)setupPageSubviews {
    _topBGView = [[UIView alloc] init];
    _topBGView.backgroundColor = MWHexColor(@"#2C3E6C");
    [self addSubview:_topBGView];
    
    _bottomBGView = [[UIView alloc] init];
    _bottomBGView.backgroundColor = MWHexColor(@"#ECF9FF");
    [self addSubview:_bottomBGView];
    
    _menuView = [[UIView alloc] initWithFrame:CGRectMake(16, 100, kScreenWidth - 32, 190)];
    _menuView.backgroundColor = MWHexColor(@"#FFFFFF");
    _menuView.layer.cornerRadius = 5;
    _menuView.layer.masksToBounds = YES;
    [self addSubview:_menuView];
    
    _avatarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _avatarBtn.tag = 0;
    _avatarBtn.layer.cornerRadius = 30;
    _avatarBtn.layer.masksToBounds = YES;
    [_avatarBtn setImage:[UIImage imageNamed:@"naodai"] forState:UIControlStateNormal];
    [_avatarBtn setImage:[UIImage imageNamed:@"naodai"] forState:UIControlStateHighlighted];
    [_avatarBtn addTarget:self action:@selector(onClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [_menuView addSubview:_avatarBtn];
    
    _nameLB = [[UILabel alloc] init];
    _nameLB.font = MWFont(18);
    _nameLB.textColor = MWHexColor(@"#333333");
    [_menuView addSubview:_nameLB];
    
    _descLB = [[UILabel alloc] init];
    _descLB.font = MWFont(12);
    _descLB.textColor = MWHexColor(@"#999999");
    [_menuView addSubview:_descLB];
    
    _homepageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _homepageBtn.tag = 1;
    _homepageBtn.titleLabel.font = MWFont(14);
    [_homepageBtn setTitle:@"个人主页" forState:UIControlStateNormal];
    [_homepageBtn setTitleColor:MWHexColor(@"#333333") forState:UIControlStateNormal];
    [_homepageBtn setImage:[UIImage imageNamed:@"ic_arrow_right"] forState:UIControlStateNormal];
    [_homepageBtn addTarget:self action:@selector(onClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [_menuView addSubview:_homepageBtn];
    
    
    _messageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _messageBtn.tag = 2;
    _messageBtn.frame = CGRectMake(0, 90, (kScreenWidth - 32) / 4, 100);
    _messageBtn.titleLabel.font = MWFont(12);
    _messageBtn.titleLabel.numberOfLines = 0;
    _messageBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_messageBtn setTitle:[[MWConfigManager manager] objectForKey:@"message_list" defaultValue:@"消息列表"] forState:UIControlStateNormal];
    [_messageBtn setTitleColor:MWHexColor(@"#333333") forState:UIControlStateNormal];
    [_messageBtn setImage:[UIImage imageNamed:@"xr_mine_message"] forState:UIControlStateNormal];
    [_messageBtn addTarget:self action:@selector(onClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [_messageBtn setIconInTopWithSpacing:10];
    [_menuView addSubview:_messageBtn];
    
    _collectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _collectBtn.tag = 3;
    _collectBtn.frame = CGRectMake((kScreenWidth - 32) / 4, 90, (kScreenWidth - 32) / 4, 100);
    _collectBtn.titleLabel.font = MWFont(12);
    _collectBtn.titleLabel.numberOfLines = 0;
    _collectBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_collectBtn setTitle:[[MWConfigManager manager] objectForKey:@"goods_collect" defaultValue:@"商品收藏"] forState:UIControlStateNormal];
    [_collectBtn setTitleColor:MWHexColor(@"#333333") forState:UIControlStateNormal];
    [_collectBtn setImage:[UIImage imageNamed:@"xr_mine_collect"] forState:UIControlStateNormal];
    [_collectBtn addTarget:self action:@selector(onClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [_collectBtn setIconInTopWithSpacing:10];
    [_menuView addSubview:_collectBtn];
    
    _languageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _languageBtn.tag = 4;
    _languageBtn.frame = CGRectMake((kScreenWidth - 32) / 4 * 2, 90, (kScreenWidth - 32) / 4, 100);
    _languageBtn.titleLabel.font = MWFont(12);
    _languageBtn.titleLabel.numberOfLines = 0;
    _languageBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_languageBtn setTitle:[[MWConfigManager manager] objectForKey:@"language_change" defaultValue:@"语言切换"] forState:UIControlStateNormal];
    [_languageBtn setTitleColor:MWHexColor(@"#333333") forState:UIControlStateNormal];
    [_languageBtn setImage:[UIImage imageNamed:@"xr_mine_language"] forState:UIControlStateNormal];
    [_languageBtn addTarget:self action:@selector(onClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [_languageBtn setIconInTopWithSpacing:10];
    [_menuView addSubview:_languageBtn];
    
    _aboutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _aboutBtn.tag = 5;
    _aboutBtn.frame = CGRectMake((kScreenWidth - 32) / 4 * 3, 90, (kScreenWidth - 32) / 4, 100);
    _aboutBtn.titleLabel.font = MWFont(12);
    _aboutBtn.titleLabel.numberOfLines = 0;
    _aboutBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_aboutBtn setTitle:[[MWConfigManager manager] objectForKey:@"about_us" defaultValue:@"关于我们"] forState:UIControlStateNormal];
    [_aboutBtn setTitleColor:MWHexColor(@"#333333") forState:UIControlStateNormal];
    [_aboutBtn setImage:[UIImage imageNamed:@"xr_mine_about"] forState:UIControlStateNormal];
    [_aboutBtn addTarget:self action:@selector(onClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [_aboutBtn setIconInTopWithSpacing:10];
    [_menuView addSubview:_aboutBtn];
}

- (void)layoutPageSubviews {
    [_topBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.equalTo(@160);
    }];
    
    [_bottomBGView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topBGView.mas_bottom);
        make.left.right.equalTo(self);
        make.height.equalTo(@140);
    }];
    
    [_menuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(100);
        make.left.equalTo(self).offset(16);
        make.right.equalTo(self).offset(-16);
        make.height.equalTo(@190);
    }];
    
    [_avatarBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.menuView).offset(16);
        make.left.equalTo(self.menuView).offset(16);
        make.width.height.equalTo(@60);
    }];
    
    [_nameLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.avatarBtn.mas_right).offset(15);
        make.right.equalTo(self.homepageBtn.mas_left).offset(-15);
        make.bottom.equalTo(self.avatarBtn.mas_centerY);
    }];
    
    [_descLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.avatarBtn.mas_centerY).offset(4);
        make.left.equalTo(self.avatarBtn.mas_right).offset(15);
    }];
    
    [_homepageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.menuView).offset(-15);
        make.centerY.equalTo(self.avatarBtn);
        make.width.equalTo(@140);
    }];
    
    [_homepageBtn setIconInRightWithSpacing:10];
}

@end
